package com.example.demofdfs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ParamPer {

	public static void main(String[] args) throws IOException {
		
		String path = ParamPer.class.getClassLoader().getResource(".").getPath();
		System.out.println(path);
		
//		if (args.length > 0)
		args = getProperties(args);
		System.out.println(Arrays.toString(args));
		com.example.demofdfs.DemoFdfsApplication.main(args);
	}
	
	
	private static String[] getProperties(String[] args) throws IOException {

		String path = null;
		for (String name : args) {
			if (name.indexOf("application.properties") > -1) {
				path = name;
				break;
			}
		}
		
		if (path == null)
			return args;
		System.out.println(path);
		File file = new File(path);
		List<String> list = new ArrayList<>();
		if (file.exists()) {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String s;
			while((s = br.readLine()) != null) {
				if (s.substring(s.indexOf("=") + 1, s.length()).trim().length() > 0)
					list.add(s);
			}
			br.close();
		}
		int l = list.size();
		int a = args.length;
		int length = l + a;
		String[] ss = new String[length];
		System.arraycopy(list.toArray(), 0, ss, 0, l);
		System.arraycopy(args, 0, ss, l, a);
		args = ss;
		return args;

	}
}
